
const Router = require("koa-router");

const router =  new Router();

const db = require("../db");

router.post("/api/login",async (ctx,next)=>{
    let {username,pass} = ctx.request.body;
    let data = await db.select(`select * from users where user="${username}" and pass="${pass}"`);
    console.log(data)
    if(data.length!=0){
        ctx.body = {
           code:0,
           msg:"登录成功",
           data:{
               id:data[0].id,
               user:data[0].user
           }
        }
    }else{
        ctx.body = {
           code:1,
           msg:"用户名或者密码错误"
        }
    }
})


router.post("/api/reg",async (ctx,next)=>{
 
     let {username,pass} = ctx.request.body;
     
     let data = await db.select(`select * from users where user="${username}"`);
   
     if(data.length!=0){
        ctx.body = {
           code:1,
           msg:"用户名已存在"
        }
        return 
     }


     let res = await db.add(`insert into users (user,pass) values ("${username}","${pass}")`);
     

     if(res==0){
        ctx.body = {
          code:0,
          msg:"注册成功"
        }
     }else{
         ctx.body = {
          code:1,
          msg:"注册失败"
        }
     }
     
})



module.exports = router;